package com.alibaba.cloud.utils;

import com.alibaba.cloud.enums.ResultCodeEnum;
import lombok.Data;

import java.io.Serializable;


/**
 * @description: 返回结果处理
 * @author: zhb1316783262@163.com
 * @date: 2018年11月26日下午6:40:30
 */
@Data
public class ResultVo<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code;

    private String info;

    private T data;


    public boolean success() {
        return this.code == ResultCodeEnum.SUCCESS.code();
    }

    public boolean fail() {
        return this.code != ResultCodeEnum.SUCCESS.code();
    }

    public static <T> ResultVo<T> common(ResultCodeEnum resultCode, T data, String message) {
        return new ResultVo<T>(resultCode.code(), message, data);
    }

    public static <T> ResultVo<T> common(int code, T data, String message) {
        return new ResultVo<T>(code, message, data);
    }

    public static <T> ResultVo<T> success(T data) {
        return new ResultVo<T>(ResultCodeEnum.SUCCESS.code(), ResultCodeEnum.SUCCESS.desc(), data);
    }

    public static <T> ResultVo<T> success(String message) {
        return new ResultVo<T>(ResultCodeEnum.SUCCESS.code(), message);
    }

    public static <T> ResultVo<T> success(T data, String message) {
        return new ResultVo<T>(ResultCodeEnum.SUCCESS.code(), message, data);
    }

    public static <T> ResultVo<T> success(ResultCodeEnum resultCode, T data, String message) {
        return new ResultVo<T>(resultCode.code(), message, data);
    }

    public static <T> ResultVo<T> error(String message) {
        return new ResultVo<T>(ResultCodeEnum.ERROR.code(), message);
    }

    public static <T> ResultVo<T> error(ResultCodeEnum resultCode, String message) {
        return new ResultVo<T>(resultCode.code(), message);
    }

    public static <T> ResultVo<T> error(int code, String message) {
        return new ResultVo<T>(code, message);
    }

    public static <T> ResultVo<T> paramError(String message) {
        return new ResultVo<T>(ResultCodeEnum.PARAMETER_ERROR.code(), message);
    }

    public static <T> ResultVo<T> paramError(ResultCodeEnum resultCode, String message) {
        return new ResultVo<T>(resultCode.code(), message);
    }

    public ResultVo() {
        super();
    }

    public ResultVo(int code, String info) {
        super();
        this.code = code;
        this.info = info;
    }

    public ResultVo(int code, String info, T data) {
        super();
        this.code = code;
        this.info = info;
        this.data = data;
    }
}
